package com.trkj.controller.hjj;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.trkj.pojo.Emppass;
import com.trkj.pojo.Role;
import com.trkj.service.ServiceException;
import com.trkj.service.hjj.GrantService;
import com.trkj.vo.EmppassVo;
import com.trkj.vo.FunctionTypeExVo;
import com.trkj.vo.Result_AddRule_Result;
import com.trkj.vo.RoleFunctionsVo;
import com.trkj.vo.RolePass;

@Controller
@SessionAttributes({"user", "funIds"})
public class GrantController {
	@Autowired
	private GrantService grantService;
	/**
	 * 修改角色的名称
	 * @param roleId
	 * @param roleName
	 * @return
	 */
	@RequestMapping("update_role_name")
	@ResponseBody
	public Result_AddRule_Result updateRole(Long roleId,String roleName){
		try {
			boolean b = grantService.updateRole(roleId, roleName);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 加载某个角色
	 * @param roleId
	 * @return
	 */
	@RequestMapping("is_load_role")
	@ResponseBody
	public Role loadRole(Long roleId){
		return grantService.loadRole(roleId);
	}
	/**
	 * 去角色界面
	 * @return
	 */
	@GetMapping("to_get_all_roles")
	public String toGetAllRoles(){
		return "hjjrole_list";
	}
	
	@GetMapping("get_all_roles")
	@ResponseBody
	public List<Role> getAllRoles(){
		return grantService.getAllRoles();
	}
	
	@PostMapping("/add_new_role")
	@ResponseBody
	public Result_AddRule_Result addNewRole(String roleName){
		try {
			grantService.addNewRole(roleName);
			return Result_AddRule_Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 查看角色用户信息
	 * @param roleId
	 * @return
	 */
	@RequestMapping("see_emppassandRole")
	@ResponseBody
	public List<EmppassVo> findRoleandStian(Long roleId){
		try {
			List<EmppassVo> list=grantService.queryByRole(roleId);
			return list;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 角色分配用户
	 * @param roleId
	 * @param passId
	 * @return
	 */
	@RequestMapping("toRoleaddEmps")
	@ResponseBody
	public Result_AddRule_Result AddRoletoEmp(RolePass r){
		try {
			grantService.grantRole(r.getRoleId(), r.getPassIds());
			return Result_AddRule_Result.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	@PostMapping("delete_role")
	@ResponseBody
	public Result_AddRule_Result deleteRole(Long roleId){
		try {
			grantService.deleteRole(roleId);
			return Result_AddRule_Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	
	@PostMapping("/get_functiontypes")
	@ResponseBody
	public List<FunctionTypeExVo> getFunctionTypeExs(long roleId){
		return grantService.getFunctionTypeExs(roleId);
	}
	
	/*@PostMapping("/grant_role")
	@ResponseBody
	public Result_AddRule_Result grantRole(RoleFunctionsVo vo){
		try {
			grantService.grantRole(vo.getRoleId(), vo.getFuncIds());
			return Result_AddRule_Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}*/
	/**
	 * 给角色授权
	 * @param vo
	 * @return
	 */
	@RequestMapping("is_giverolesq")
	@ResponseBody
	public Result_AddRule_Result isGiveRoleSQ(RoleFunctionsVo vo){
		try {
			grantService.GrantRoleandFun(vo.getRoleId(), vo.getFuncIds());
			return Result_AddRule_Result.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	
/*	@PostMapping("/login")
	@ResponseBody
	public Result_AddRule_Result login(Long id, String pass, Model model){
		Emppass user = grantService.login(id, pass);
		if(user == null){
			//登陆失败
			return Result_AddRule_Result.failure("对不起，登录失败，可能是用户名或密码错误，或者没有登陆权限！");
		}else{
			//登陆成功
			model.addAttribute("user", user);
			model.addAttribute("funIds", grantService.getFunIdsByUserId(user.getPassId()));
			return Result_AddRule_Result.SUCCESS;
		}
	}
	
	@GetMapping("/to_login")
	public String toLogin(){
		return "login";
	}*/
}
